package cc.chinagps.gateway.client.bean;

import cc.chinagps.gateway.unit.eg.pkg.EGPackage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;

@Data
public class DieselRealTimeInfo {
    private int supplyDataLen = 17;
    private int smokeDetectLen = 8;
    private int suZhouInfoLen = 23;

    private ObdBaseInfo obdBaseInfo;
    private ObdEngineDataStreamInfo dataStreamInfo;

    public void decode(EGPackage pkg) throws Exception {
        byte[] data = pkg.getData();
        ByteBuf byteBuf = Unpooled.copiedBuffer(data);
        int position = 0;
        position += 6;
        position += 2;
        byteBuf.skipBytes(8);
        dataStreamInfo = new ObdEngineDataStreamInfo();
        while (position < data.length) {
            if (position == data.length - 1) {
                break;
            }
            int infoType = data[position++] & 0xFF;
            byteBuf.skipBytes(1);
            switch (infoType) {
                case 0x01:
                    obdBaseInfo = new ObdBaseInfo();
                    obdBaseInfo.parse(byteBuf, position);
                    position += obdBaseInfo.getDataLen();
                    break;
                case 0x02:
                    dataStreamInfo.parse(byteBuf);
                    position += dataStreamInfo.getDataLen();
                    break;
                case 0x80:
                    dataStreamInfo.setHasSupplyData(true);
                    dataStreamInfo.parseSupply(byteBuf);
                    position += supplyDataLen;
                    break;
                case 0x90:
                    position += smokeDetectLen;
                    byteBuf.skipBytes(smokeDetectLen);
                    break;
                case 0x91:
                    position += 9;
                    byteBuf.skipBytes(9);
                    break;
                case 0x92:
                    position += suZhouInfoLen;
                    byteBuf.skipBytes(suZhouInfoLen);
                    break;
                default:
                    break;
            }
        }
    }
}
